#include "StdTcp.h"
#include "ClientFunc.h"
#include "StdThread.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

static char MyName[NameSize]; 

void AnalysisRecv(MSG *m)
{
    switch (m->flag)
    {
    case CHAT:
    case BROADCAST:
        printf("接收到来自|%s|的消息:%s\n",m->FromName,m->Content);
        break;
    case ONLINELIST:
        ShowOnlineList(m);
        break;
    case SENDFILE:
        WriteToLocal(m);
        break;
    default:
        break;
    }
}

void * ThreadHandle(void* arg)
{
    TcpC *c = (TcpC*)arg;
    while(1)
    {
        MSG m;
        if(TcpClientRecv(c,&m,sizeof(m)) == false)
        {
            printf("读线程接受错误!\n");
            exit(-1);
        }
        AnalysisRecv(&m);
    }
}


void LoginSignUp(TcpC *c)
{
    while(1)
    {
        int choice = LoginFace();
        MSG m;
        switch (choice)
        {
        case LOGIN:
            m = Login(MyName);
            TcpClientSend(c,&m,sizeof(MSG));
            if(TcpClientRecv(c,&m,sizeof(MSG)) == false)
                exit(-1);
            if(IsLoginSuccess(&m) == true)
                return;
            break;
        case SIGNUP:
            m = SignUp(MyName);
            TcpClientSend(c,&m,sizeof(MSG));
            if(TcpClientRecv(c,&m,sizeof(MSG)) == false)
                exit(-1);
            if(IsSignUpSuccess(&m) == true)
                return;
            break;
        default:
            break;
        }
        sleep(2);
        system("clear");
    }
}

void SendFileToServer(MSG *m,TcpC *c)
{
    char *fileName = GetFileNameFromPath(m->Content);
    PackFile *pf = PacketFileInit(m->Content);
    
    memset(m->Content,0,sizeof(m->Content));
    strcpy(m->Content + 4,fileName);
    int size = 1000;
    int count = 0;
    while(IsFileEnd(pf) == false)
    {
        ReadFromFile(pf,m->Content + 24,&size);
        //填入读取的文件内容大小
        memcpy(m->Content,&size,sizeof(int)); 
        TcpClientSend(c,m,sizeof(MSG));
        count++;
        printf("msg flag : %d   这是第%d包数据,大小:%d\n",m->flag,count,size);
    }
    free(fileName);
    ClearPacketFile(pf);
}

int main(int argc,char *argv[])
{
    // if(argc != 3)
    // {
    //     printf("参数数量不对\n");
    //     return -1;
    // }
    // TcpC *c = TcpClientInit(argv[1],atoi(argv[2]));
    TcpC *c = TcpClientInit("192.168.27.129",8085);
    LoginSignUp(c);
    

    Thread *t = ThreadInit(ThreadHandle,c);
    while(1)
    {
        int choice = Interface();
        MSG m;
        switch (choice)
        {
        //私聊
        case CHAT:
            m = ChatToClient();
            strcpy(m.FromName,MyName);
            TcpClientSend(c,&m,sizeof(m));
            break;
        //广播
        case BROADCAST:
            m = ChatBroadCast();
            strcpy(m.FromName,MyName);
            TcpClientSend(c,&m,sizeof(m));
            break;
        case ONLINELIST:
            m = GetServerOnlineList();
            strcpy(m.FromName,MyName);
            TcpClientSend(c,&m,sizeof(m));
            break;
        case SENDFILE:
           m = SendFile();
           strcpy(m.FromName,MyName);
           SendFileToServer(&m,c);
           break;
        case MYCLOUD:
            while(1)
            {
                int choice = CloudInterface();
                switch (choice)
                {
                    case FILELIST:
                    {
                        break;
                    }
                    case UPLOAD:
                    {
                        MSG m = UpLoadFile();
                        strcpy(m.FromName,MyName);
                        SendFileToServer(&m,c);
                        break;
                    }
                    case DOWNLOAD:
                    {
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
        default:
            break;
        }
    }

    return 0;
}